package cn.iocoder.yudao.module.member.service.leaveword;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.leaveword.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.leaveword.LeaveWordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.leaveword.LeaveWordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户留言 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LeaveWordServiceImpl implements LeaveWordService {

    @Resource
    private LeaveWordMapper leaveWordMapper;

    @Override
    public Long createLeaveWord(LeaveWordSaveReqVO createReqVO) {
        // 插入
        LeaveWordDO leaveWord = BeanUtils.toBean(createReqVO, LeaveWordDO.class);
        leaveWordMapper.insert(leaveWord);
        // 返回
        return leaveWord.getId();
    }

    @Override
    public void updateLeaveWord(LeaveWordSaveReqVO updateReqVO) {
        // 校验存在
        validateLeaveWordExists(updateReqVO.getId());
        // 更新
        LeaveWordDO updateObj = BeanUtils.toBean(updateReqVO, LeaveWordDO.class);
        leaveWordMapper.updateById(updateObj);
    }

    @Override
    public void deleteLeaveWord(Long id) {
        // 校验存在
        validateLeaveWordExists(id);
        // 删除
        leaveWordMapper.deleteById(id);
    }

    private void validateLeaveWordExists(Long id) {
        if (leaveWordMapper.selectById(id) == null) {
            throw exception(LEAVE_WORD_NOT_EXISTS);
        }
    }

    @Override
    public LeaveWordDO getLeaveWord(Long id) {
        return leaveWordMapper.selectById(id);
    }

    @Override
    public PageResult<LeaveWordDO> getLeaveWordPage(LeaveWordPageReqVO pageReqVO) {
        return leaveWordMapper.selectPage(pageReqVO);
    }

}